<?php
// +----------------------------------------------------------------------
// | Created by Sublime: [ Conqweal ]
// +----------------------------------------------------------------------
// | Copyright (c) 2021~20223[chenjia] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.99dn.cn
// +----------------------------------------------------------------------
// | Author: Conqweal <150672834@qq.com>
// +----------------------------------------------------------------------
// | Date: 2021/12/22 0026
// +----------------------------------------------------------------------
// | Description:任务外链
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\admin\model\MissionLinks as cmodel;
use app\BackendController;
use support\Request;
use think\facade\Db;

class MissionLinksController extends BackendController
{

    protected $pushUrl = 'http://192.168.1.12:89/api/Hprose/start';
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $status                 = cmodel::getStatus();
        $status_options         = output_select($status, []);
        $mission_status_options = output_select(\app\admin\model\Mission::getStatus(), []);
        return view('missionlinks/index', compact('status_options', 'mission_status_options'));
    }

    /**
     * 批量推送
     * @return [type] [description]
     */
    public function batchPush(Request $request)
    {

        require base_path() . "/extend/Hprose/Hprose.php";
        $client = new \Hprose\Http\Client($this->pushUrl, false);

        $ids = $request->get('id');

        Db::startTrans();
        try {
            $r = cmodel::alias('a')->join('mission m', 'm.mission_id=a.mission_id')->where('a.mission_id', 'in', $ids)->where('a.status', 1)->where('m.status', '>', 3)->select();
            foreach ($r as $v) {
                $v->save(['status' => 2]);
                $client->updatePush($v->push_id);
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return $this->error($e->getMessage());
        }
        return json(['status' => 1, 'msg' => '推送成功']);
    }

    /**
     * 列表json数据
     * @param  Request $request [请求变量]
     * @return [type]           [json数据]
     */
    public function listData(Request $request)
    {
        $limit = $request->get('limit', 10);
        $w     = [];

        if ($request->get('title')) {
            $w[] = ['b.title', 'like', '%' . $request->get('title') . '%'];

        }

        if ($request->get('href')) {
            $w[] = ['a.links_data->href', 'like', '%' . $request->get('href') . '%'];

        }

        if ($request->get('create_time')) {
            $w[] = ['a.create_time', '>=', strtotime($request->get('create_time'))];

        }

        if (null != $request->get('status')) {
            $w[] = ['a.status', '=', $request->get('status')];

        }

        if (null != $request->get('mission_status')) {
            $w[] = ['m.status', '=', $request->get('mission_status')];

        }

        $q = cmodel::alias('a')->join("mission m", "a.mission_id=m.mission_id", 'left')->join("mission_description b", "a.mission_id=b.mission_id", 'left')->field("a.*,m.status as mission_status,b.title")->where($w);
        $q->order("a.id", "desc");

        $ls     = $q->paginate(['list_rows' => $limit, 'request' => $request->all()]);
        $res    = $ls->items();
        $status = cmodel::getStatus();

        $mission_status = \app\admin\model\Mission::getStatus();

        $res = array_map(function ($v) use ($status, $mission_status) {
            $v->status_txt         = $status[$v->status];
            $v->mission_status_txt = $mission_status[$v->mission_status];
            $v->user_info          = $v->user_info;
            $v->profile            = $v->user_info->profile;
            return $v;
        }, $res);

        return json(['code' => $ls->total() > 0 ? 0 : 201, 'msg' => '无数据', 'count' => $ls->total(), 'data' => $res]);
    }

}
